import { _HttpClient } from '@delon/theme';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { Observable } from 'rxjs';
import { CourseTeacherDTO } from 'src/app/project/model/icm/icm-start-course/start-course/course-teacher.dto';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class CourseTeacherService {
  private static URL = '/api/icm-course-teacher';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询课程开课信息
   * @param option 分页参数
   * @param year 所属年度
   * @param termType 所属学期
   * @param courseName 课程名称
   * @param teacherName 授课教师名称
   * @param isOpen 是否开课
   */
  findOnePage(
    option: QueryOptions,
    year?: string,
    termType?: string,
    courseName?: string,
    teacherName?: string,
    isOpen?: boolean,
  ): Observable<ApiPagedData<CourseTeacherDTO>> {
    const params = {};
    Object.assign(
      params,
      option,
      year ? { year } : {},
      termType ? { termType } : {},
      courseName ? { courseName } : {},
      teacherName ? { teacherName } : {},
      isOpen !== null ? { isOpen } : {},
    );
    return this.http.get<ApiPagedData<CourseTeacherDTO>>(CourseTeacherService.URL, params);
  }

  /**
   * 添加课程开课信息
   * @param courseTeacherDTO 课程开课信息
   */
  create(courseTeacherDTO: CourseTeacherDTO): Observable<CourseTeacherDTO> {
    return this.http.post<CourseTeacherDTO>(CourseTeacherService.URL, courseTeacherDTO);
  }

  /**
   * 根据id查找课程开课信息
   * @param id 课程开课信息id
   */
  findById(id: string): Observable<CourseTeacherDTO> {
    return this.http.get<CourseTeacherDTO>(`${CourseTeacherService.URL}/${id}`);
  }

  /**
   * 修改更新课程开课信息
   * @param id 课程开课id
   * @param courseTeacherDTO 课程开课信息
   */
  complete(id: string, courseTeacherDTO: CourseTeacherDTO): Observable<CourseTeacherDTO> {
    return this.http.put<CourseTeacherDTO>(`${CourseTeacherService.URL}/${id}`, courseTeacherDTO);
  }

  /**
   * 根据id删除课程开课
   * @param id 课程开课id
   */
  delete(id: string): Observable<CourseTeacherDTO> {
    return this.http.delete<CourseTeacherDTO>(`${CourseTeacherService.URL}/${id}`);
  }

  /**
   * 查找所有课程开课，可按需求排序
   * @param param 排序参数
   * @param pattern 排序模式: asc/desc
   */
  findAllBySort(param?: string, pattern?: string): Observable<Array<CourseTeacherDTO>> {
    const params = {};
    Object.assign(params, param ? { param } : {}, pattern ? { pattern } : {});
    return this.http.get<Array<CourseTeacherDTO>>(`${CourseTeacherService.URL}/all`, params);
  }
}
